//
//  OJSearchIssueListController.m
//  openJira
//
//  Created by Vincent Demay on 05/05/10.
//  Copyright 2010 Goojet. All rights reserved.
//

#import "OJSearchIssueListController.h"


@implementation OJSearchIssueListController
@synthesize isSearching;

- (id)initWithStyle:(UITableViewStyle)style {
    if (self = [super initWithStyle:style]) {
		
		UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"minilogo.png"]];
		self.navigationItem.titleView = imageView;
		[imageView release];
		
		UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
		temporaryBarButtonItem.title = @"Search";
		self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
		self.navigationController.navigationBar.contentMode = UIViewContentModeScaleAspectFit;
		[temporaryBarButtonItem release];
		
		//self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"create" style:UIBarButtonItemStyleBordered target:self action:@selector(onCreateTouch:)];
		
		self.tabBarItem.title = @"Search";
		self.tabBarItem.image = [UIImage imageNamed:@"search.png"];
		
		self.title = @"Search";
		
		self.hidesBottomBarWhenPushed = NO;
		
		[self.tableView setScrollEnabled:YES];
		self.tableView.allowsSelection = NO;
	}
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
	
	searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
	searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
	searchBar.delegate = self;
	searchBar.tintColor = BG_COLOR;
	
	self.tableView.tableHeaderView = searchBar;
}

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Set up the cell...
	
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark UISearchBarDelegate
- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar {
	//Add the overlay
	if (!overlay) {
		overlay = [[OverlayViewController alloc] init];
		overlay.rvController = self;
	}
	//compute overlay frame
	CGFloat yaxis = self.navigationController.navigationBar.frame.size.height;
	CGFloat width = self.view.frame.size.width;
	CGFloat height = self.view.frame.size.height;
	CGRect frame = CGRectMake(0, yaxis, width, height);
	overlay.view.frame = frame;
	//set it as grey
	overlay.view.backgroundColor = [UIColor grayColor];
	overlay.view.alpha = 0.5;
	//put it
	[self.tableView insertSubview:overlay.view aboveSubview:self.parentViewController.view];
	
	
	//searching = YES;
	letUserSelectRow = NO;
	self.tableView.scrollEnabled = NO;
	
	//Add the cancel button.
	[searchBar setShowsCancelButton:YES animated:YES];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *) theSearchBar {
	[self searchCanceled:nil];
}

-(void) searchCanceled:(id) sender {
	activity.hidden = YES;
	[searchBar setShowsCancelButton:NO animated:YES];
	[searchBar resignFirstResponder];
	[overlay.view removeFromSuperview];
	letUserSelectRow = YES;
}

- (void) searchBarSearchButtonClicked:(UISearchBar *)theSearchBar {
	isSearching = YES;
	
	if (!activity) {
		activity = [[TTActivityLabel alloc] initWithStyle:TTActivityLabelStyleBlackBezel];
		activity.text = @"Searching...";
		[activity sizeToFit];
	}
	activity.hidden = NO;
	activity.frame = CGRectMake(0, 0, overlay.view.frame.size.width, 180);
	[overlay.view addSubview:activity];
	activity.isAnimating = YES;
}

- (void)dealloc {
    [super dealloc];
}


@end





/////////////////////// OVERLAY //////////////////////////
//OverlayViewController.m
@implementation OverlayViewController

@synthesize rvController;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	if (!rvController.isSearching) {
		[rvController searchCanceled:nil];
	}
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
}


- (void)dealloc {
	[OJSearchIssueListController release];
	[super dealloc];
}
@end

